package com.earnings.okhttputils.utils.ui.activity.common;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.RecordData;
import com.earnings.okhttputils.utils.bean.RecordListData;
import com.earnings.okhttputils.utils.bean.UserHelpTypeData;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.ui.dialog.BillDialog;
import com.earnings.okhttputils.utils.utils.SwipeRefresh;
import com.earnings.okhttputils.utils.utils.TimestampTransform;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static com.earnings.R.id.recyclerview;
import static com.earnings.R.id.view;
import static com.earnings.okhttputils.utils.UrlConstant.Constant.RECORD_TYPE_PAY_OFFLINE;


/*
收款页面-收款记录
 */
public class RecordActivity extends GodLeftHandBaseActivity implements GodOnClickListener {


    private ArrayList<RecordListData> datas;
    private ArrayList<UserHelpTypeData> typedatas;
    private BillDialog bill;
    private GodBaseAdapter adapter;

    private int orderTypeId = Constant.RECORD_TYPE_ALL;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );

    }

    @Override
    protected void init() {
        setTitle( "账单" );
        if (bundleData.containsKey( "title" )) {
            setTitle( bundleData.getString( "title" ) );
        }
        if (bundleData.containsKey( "type" )) {
            orderTypeId = bundleData.getInt( "type" );

        } else {
            showRightTextView( "筛选" );
            setOnClickListener( this, R.id.top_right_tv ,R.id.cont);

        }
        datas = new ArrayList<RecordListData>();
        bill = new BillDialog( getContext(), findViewById( R.id.top_view ) ) {
            @Override
            public void onClick(String id, String text) {
                orderTypeId = Integer.parseInt( id );

                getView( R.id.cont ).setVisibility(View.GONE );
                page = 1;
                datas.clear();
                loadData();
            }
        };
        bill.showRecord();
        loadTypeData();
        initDatas();
        new SwipeRefresh( this, R.id.refresh, R.id.recyclerview ) {
            @Override
            public void pullDown() {

                page = 1;
                datas.clear();
                loadData();
            }

            @Override
            public void pullUp() {
                page++;
                loadData();
            }
        };
    }


    public void initDatas() {
        switch (orderTypeId) {
            case Constant.RECORD_TYPE_MONEY_TURN:
                setTitle( "转账记录" );
                break;
            case Constant.RECORD_TYPE_MONEY_RETURN:
                setTitle( "收款记录" );
                break;
        }
    }

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_recyclerview_refresh;
    }


    @Override
    public void OnClickListener(View view) {
        if (view.getId() == R.id.top_right_tv) {


            bill.show();
            getView( R.id.cont ).setVisibility(View.VISIBLE );
        } if (view.getId() == R.id.cont) {


            getView( R.id.cont ).setVisibility(View.GONE );
            bill.dismiss();
            //getView( R.id.cont ).setVisibility(View.VISIBLE );
        }
    }

    int page = 1;

    public void loadData() {
        //   datas.clear();
        //showProgress();
        Map map = new HttpMap( getContext() );
        map.put( "orderTypeid", orderTypeId + "" );
        map.put( "page", page + "" );

        OkHttpUtils.post().url( HttpUrl.USER_ORDER_LIST_URL ).params( map ).build().execute( new HttpArrayCallback<RecordData>( getContext() ) {
            @Override
            public void onSuccess(List<RecordData> object, String msg) {
                for (int i = 0; i < object.size(); i++) {
                    List<RecordListData> list = object.get( i ).getBill();
                    datas.addAll( list );

                }
                if (adapter == null)
                    initData();
                else {
                    adapter.notifyDataSetChanged();
                }
                dismissProgress();
                // page++;
                //  datas = (ArrayList<RecommendFriendsListData>) object.getList();
                //  initData();
            }

            @Override
            public void onFail(int code, String msg) {

                ToastUtils.showToast( getContext(), msg );
                dismissProgress();
            }


        } );


    }

    public void initData() {
        adapter = new GodBaseAdapter<RecordListData>( R.layout.item_recesovable_record, datas ) {
            @Override
            public void onBind(GodViewHolder holder, int position, RecordListData data) {
                if (position != 0) {
                    if (!data.getAdd_date().equals( datas.get( position - 1 ).getAdd_date() ))
                        holder.setVisibility( R.id.mouth, View.VISIBLE );
                    else holder.setVisibility( R.id.mouth, View.GONE );
                } else
                    holder.setVisibility( R.id.mouth, View.VISIBLE );

                holder.setText( R.id.mouth, data.getAdd_date() );
              //  holder.setText( R.id.name, data.getNote() );
                if(data.getMoney().indexOf("-")>-1){
                    holder.setText( R.id.price, data.getMoney() );
                    holder.setTextColor(R.id.price,getResources().getColor(R.color.textred));}
                else    {
                    holder.setText( R.id.price, "+"+data.getMoney() );
                    holder.setTextColor(R.id.price,getResources().getColor(R.color.style));
                }
                holder.setText( R.id.info, data.getProfile() );
                holder.setText( R.id.date, TimestampTransform.TimeStamp2Date( data.getAdd_time() ) );
                Glide.with( getContext() ).load( data.getIcon() ).into( (ImageView) holder.getView( R.id.icon ) );
                //holder.setText( R.id.name,data.getNote() );
            }

            @Override
            public void OnItemClickListener(View view, int position, RecordListData data) {
                super.OnItemClickListener( view, position, data );
                int type = Integer.parseInt( data.getOrdertypeid() );
                bundleData.putInt( "type", type );
                if (type == Constant.RECORD_TYPE_MONEY_TURN || type == Constant.RECORD_TYPE_MONEY_RETURN ||
                        type == Constant.RECORD_TYPE_UP_LEAVEL || type == Constant.RECORD_TYPE_RECHARGE_ONLINE || type == RECORD_TYPE_PAY_OFFLINE
                        || type == Constant.RECORD_TYPE_RECHARGE_PHONE || type == Constant.RECORD_TYPE_RECHARGE_FLOW || type == Constant.RECORD_TYPE_RECHARGE_OIL ||
                        type == Constant.RECORD_TYPE_WITHDRAWALS || type == Constant.RECORD_TYPE_RED){
                    bundleData.putString( "id", data.getId() );
                skipActivity( RecordDetailsActivity.class );}
            }
        };

        setRecyclerViewLayoutManager( recyclerview, new LinearLayoutManager( this ) ).setAdapter( adapter );
    }

    public void loadTypeData() {
        showProgress();
        Map map = new HttpMap( getContext() );

        OkHttpUtils.post().url( HttpUrl.USER_ORDER_TYPE_URL ).params( map ).build().execute( new HttpArrayCallback<UserHelpTypeData>( getContext() ) {
            @Override
            public void onSuccess(List<UserHelpTypeData> object, String msg) {
                typedatas = (ArrayList<UserHelpTypeData>) object;

                bill.initData( typedatas );

                loadData();
                dismissProgress();
                //  dismissProgress();
                //  datas = (ArrayList<RecommendFriendsListData>) object.getList();
                //  initData();
            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
                ToastUtils.showToast( getContext(), msg );//dismissProgress();
            }


        } );


    }


}
